package com.zby.scms.controller;

import com.zby.scms.entity.Scmsinfo;
import com.zby.scms.service.ScmsinfoService;
import com.zby.scms.utils.BaseResult;
import com.zby.scms.utils.FileUtil;
import com.zby.scms.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.*;

/**
 * (Scmsinfo)表控制层
 *
 * @author zhangboyi
 * @since 2020-01-29 16:21:26
 */
@Api(tags = "(Scmsinfo)") 
@RestController
@RequestMapping("scmsinfo")
public class ScmsinfoController {
    /**
     * 服务对象
     */
    @Autowired
    private ScmsinfoService scmsinfoService;
    private Logger logger = LoggerFactory.getLogger(ScmsinfoController.class);

    @Value ("${web.upload-path}")
    private String filePath;
    @Value("${web.domain}")
    private String domain;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询 ")
    @GetMapping("selectOne/{id}")
    public Scmsinfo selectOne(@ApiParam(value = "infoId ID") @PathVariable("id") Integer id) {
        return this.scmsinfoService.queryById(id);
    }

    @ApiOperation(value = "添加咨询或者公告" )
    @PostMapping("addInfo")
    public BaseResult<Scmsinfo>  addInfo(Scmsinfo scmsinfo,@RequestParam(value = "file") MultipartFile file){
        if(scmsinfo.getInfotype()==null){
            return ResultUtil.error(400, "信息类型不能为空");
        }
/*        String fileName = file.getOriginalFilename();  // 文件名
        logger.info(fileName);
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        // 上传后的路径
        String formatPath = filePath;

        File dest = new File(formatPath + fileName);//文件上传的目的地
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        FileUtil fileUtil = new FileUtil();

        scmsinfo.setCreatetime(new Date());
        scmsinfo.setCover(fileUtil.uploadFile(file));
        return ResultUtil.success(scmsinfoService.addScmsinfo(scmsinfo));
    }

    @ApiOperation(value = "修改咨询或者公告" )
    @PutMapping("updateInfo")
    public BaseResult<Scmsinfo> updateInfo(Scmsinfo scmsinfo){


        scmsinfo.setUpdatetime(new Date());
        return ResultUtil.success(scmsinfoService.updateScmsinfo(scmsinfo));
    }

    @ApiOperation(value = "查询所有" )
    @GetMapping("queryInfo" )
    public BaseResult<Scmsinfo> queryInfo(String infoType){
        //类型+标签
        if(infoType==null){
            return ResultUtil.success(/*scmsinfoService.queryAll*/);
        }
        return ResultUtil.success();
    }







    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        logger.info(fileName);
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        // 上传后的路径
        String filePath = "E://编程资料/IDEA Projects/scms/src/main/resources/uploadFiles/";
//        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
            logger.info("hhhh");
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        String filename = "/temp-rainy/" + fileName;
        model.addAttribute("filename", fileName);
        return dest.getAbsolutePath();
    }





}